package nnweboa;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;

public class DB {
	public static void UpdateuserInfo(Userinfo uinfo) throws SQLException,
			InstantiationException, IllegalAccessException,
			ClassNotFoundException {
		String presql = "update userinfo  set mail=?,name=?,password=?,userright=?,modifydate=?,freeze=? where id=?";
		PreparedStatement sql = SystemConfig.getInstance().getConnection()
				.prepareStatement(presql);
		sql.setString(1, uinfo.getMail());
		sql.setString(2, uinfo.getName());
		sql.setString(3, uinfo.getPassword());
		sql.setInt(4, uinfo.getRight());
		sql.setTimestamp(5, uinfo.getModifyTime());
		sql.setInt(6, uinfo.getFreeze());
		sql.setLong(7, uinfo.getId());
		sql.executeUpdate();

	}

	public static void InsertUser(String mail, String workname, String right,
			String password) throws SQLException, InstantiationException,
			IllegalAccessException, ClassNotFoundException {
		String sql = "insert  into userinfo values (NULL,?,?,?,?,?,?)";
		PreparedStatement presql = SystemConfig.getInstance().getConnection()
				.prepareStatement(sql);
		presql.setString(1, mail);
		presql.setString(2, password);
		presql.setString(3, workname);
		presql.setInt(4, Integer.parseInt(right));
		Timestamp tp = new Timestamp(Calendar.getInstance().getTimeInMillis());
		presql.setTimestamp(5, tp);
		presql.setInt(6, 0);
		presql.executeUpdate();

	}

	public static void DeleteUser(Userinfo userinfo) throws SQLException,
			InstantiationException, IllegalAccessException,
			ClassNotFoundException {
		if (userinfo.getRight() == User.NTManager) {
			return;
		}
		String sql = "delete from  userinfo where id=?";
		PreparedStatement presql = SystemConfig.getInstance().getConnection()
				.prepareStatement(sql);
		presql.setLong(1, userinfo.getId());

		presql.executeUpdate();
	}
}
